package com.sun.springCache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

public class PersonService {
	private static int flag = 0;
	
	@Autowired
	private PersonDao dao;
	@Cacheable(value="personCache", key="#id")
	public Person getPersonById(String id) {
		System.out.println(id + "--被缓存了");
		return dao.getPersonById(id);
	}
	
	@Cacheable(value="personCache1", key="#id", condition = "#id != '2'")
	public Person getPersonById1(String id) {
		System.out.println(id + "--被缓存了,--2不会被缓存");
		return dao.getPersonById(id);
	}
	
	@CachePut(value="personCache2", key="#id+'#'+#name")
	public Person getPersonById2(final String id, final String name) {
		System.out.println(id + "--被缓存了,但是还是会执行方法体" + flag++);
		return dao.getPersonById(id);
	}
	
	@Cacheable(value="personCache3", key="#person.id")
	public Person getPerson(Person person) {
		System.out.println(person.getId() + "--被缓存了");
		return dao.getPersonById(person.getId());
	}
	
	@CacheEvict(value = {"personCache"},allEntries=true)
	public void clear() {
		System.out.println("所有缓存都被清理了！");
	}
}
